package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.comon.UserHolder;
import com.itheima.reggie.domain.AddressBook;
import com.itheima.reggie.domain.OrderDetail;
import com.itheima.reggie.domain.Orders;
import com.itheima.reggie.domain.ShoppingCart;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 七安
 * @description 针对表【orders(订单表)】的数据库操作Service实现
 * @createDate 2022-08-15 16:15:08
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
        implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;

    //保存订单
    @Override
    public void saveOrder(Orders orders) {
        //生成id
        Long orderId = IdWorker.getId();
        //添加订单明细
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, UserHolder.get().getId());
        List<ShoppingCart> cartList = shoppingCartService.list(wrapper);
        //计算总价
        BigDecimal sum = new BigDecimal("0");
        if (CollectionUtil.isNotEmpty(cartList)) {
            for (ShoppingCart shoppingCart : cartList) {
                OrderDetail orderDetail = new OrderDetail();
                //设置订单表id
                orderDetail.setOrderId(orderId);
                //将购物车中的信息添加到订单明细中
                BeanUtils.copyProperties(shoppingCart, orderDetail);
                sum = sum.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber().toString())));
                //将订单明细添加到数据库
                orderDetailService.save(orderDetail);
            }
        }
        //添加订单
        orders.setId(orderId);
        //添加订单号
        orders.setNumber("fuCheng" + orderId);
        //添加用户id
        orders.setUserId(UserHolder.get().getId());
        //添加总价
        orders.setAmount(sum);
        //获取地址
        AddressBook addressBook = addressBookService.getById(orders.getAddressId());
        //设置手机号
        orders.setPhone(addressBook.getPhone());
        //设置详细地址
        orders.setAddress(addressBook.getDetail());
        //设置用户名
        orders.setUserName(UserHolder.get().getName());
        //设置收货人
        orders.setConsignee(addressBook.getConsignee());
        //添加到数据库
        ordersMapper.insert(orders);
        //清空购物车
        LambdaQueryWrapper<ShoppingCart> cartWrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, UserHolder.get().getId());
        shoppingCartService.remove(cartWrapper);
    }

    @Override
    public Page<Orders> findOrders(Integer page, Integer pageSize) {
        //分页查询
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, UserHolder.get().getId());
        wrapper.orderByDesc(Orders::getOrderTime);
        Page<Orders> ordersPage = new Page<Orders>(page, pageSize);
        ordersPage = ordersMapper.selectPage(ordersPage, wrapper);
        //遍历集合
        List<Orders> ordersList = ordersPage.getRecords();
        for (Orders orders : ordersList) {
            //根据id查到当前订单的详情
            LambdaQueryWrapper<OrderDetail> odWrapper = new LambdaQueryWrapper<>();
            odWrapper.eq(OrderDetail::getOrderId, orders.getId());
            List<OrderDetail> orderDetails = orderDetailService.list(odWrapper);
            //将查询到的结果封装到orders中
            orders.setOrderDetails(orderDetails);
        }
        return ordersPage;
    }

    //后台分页查询
    @Override
    public Page<Orders> findOrdersByManage(Integer page, Integer pageSize, String number, Date beginTime, Date endTime) {
        //创建条件
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(number), Orders::getNumber, number)
                .between(beginTime != null, Orders::getCheckoutTime, beginTime, endTime);
        //分页查询
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        ordersPage = ordersMapper.selectPage(ordersPage, wrapper);
        List<Orders> ordersList = ordersPage.getRecords();
        if (CollectionUtil.isNotEmpty(ordersList)) {
            for (Orders orders : ordersList) {
                //根据id查询订单明细
                LambdaQueryWrapper<OrderDetail> odWrapper = new LambdaQueryWrapper<>();
                odWrapper.eq(OrderDetail::getOrderId, orders.getId());
                List<OrderDetail> orderDetails = orderDetailService.list(odWrapper);
                orders.setOrderDetails(orderDetails);
            }
        }
        return ordersPage;
    }
}




